#!/usr/bin/python

from math import abs
from random import shuffle
import ansi



baseRow = [rook, knight, bishop, queen, king, bishop, knight, rook]
pawnRow = [pawn] * 8
blankRow = [blank] * 8


class Chess(object):
    # Stores all the rules of chess based on a 2D List of chars
    # All indexing is in (row, column) format
    # Moves are formatted by ((y1, x1), (y2, x2))

    class Piece(object):
        def __init__(self, name, colour, value, char):
            self.name = name
            self.colour = colour
            self.value = value
            self.ascii = colour + char + endc

        def __str__(self, other):
            return self.ascii

        def __lt__(self, other):
            return self.value < other.value

        def __eq__(self, other):
            return self.value == other.value

        def __gt__(self, other):
            return self.value > other.value

        def __ne__(self, other):
            return self.value != other.value
            
    king = Piece("king", None, 0, ansi.king)
    queen = Piece("queen", None, 9, ansi.queen)
    rook = Piece("rook", None, 5, ansi.rook)
    bishop = Piece("bishop", None, 3, ansi.bishop)
    knight = Piece("knight", None, 3, ansi.knight)
    pawn = Piece("pawn", None, 1, ansi.pawn)    

    def __init__(self, random=False):
        self.board = [[colourise(char, red) for char in baseRow]]
        self.board += [[colourise(char, red) for char in pawnRow]]
        self.board += [blankRow] * 4
        if random:
            self.board = [shuffle(row) for row in board]

    def __getitem__(self, position):
        return self.board[position[0]][position[1]]

    def 

    def isLegal(self, move):
        
    def makeMoves(self, move1, move2):
        piece1 = self.board[move1[0]][move1[1]]
        piece2 = self.board[move2[0]][move2[1]]
        if move1[1] == move2[1]: # same destination
             if pieceValue[piece1] == pieceValue[piece2]: # same piece
                 self.board[move1[0]][move

    def printBoard(self):
        print("    + " + "- + " * 8)
        rowIndex = 8
        for row in self.board:
            print(str(rowIndex) + "   | " + " | ".join(row) + " |")
            print("    + " + "- + " * 8)
            rowIndex -= 1
        print("\n      A   B   C   D   E   F   G   H")

def colourise(char, colour):
    return colour + char + endc

def squareToIndex(square):
    horizontalIndex = horizontal.index(square[0])
    verticalIndex = 8 - int(square[1])
    return (verticalIndex, horizontalIndex)

def horizontalLine(initial, final):
    return initial[0] == final[0]

def verticalLine(initial, final):
    return initial[1] == final[1]

def diagonalLine(initial, final):
    return abs(initial[0] - final[0]) == abs(initial[1] - final[1])

def path(piece, initial, final):
    # returns list of squares in between (not-inclusive), returns false if illegal move
    if not all([0 <= index <= 7 for index in (initial + final)]):
        return False
    if piece == "K":
        if (initial[0] - final[0]) ** 2 + (initial[1] - final[1]) ** 2 < 3:
            return []
        else:
            return False
    elif piece == "Q":
        if any(fmap((horizontalLine, verticalLine, diagonalLine), (initial, final))):


pawnRow = [pawn] * 8
backRow = [rook, knight, bishop, queen, king, bishop, knight, rook]
emptyRow = [" "] * 8

board = [colorise(backRow, red), colorise(pawnRow, red)] + [emptyRow] * 4 + [colorise(pawnRow, cyan), colorise(backRow, cyan)]
board = [[item for item in line] for line in board]

printBoard(board)

turns = ("cyan", "red")
whoseTurn = 0
players = (raw_input("Who will play for cyan? "), raw_input("Who will play for red? "))
while True:
    initial = []
    destination = []
    for current in (0, 1):
        print "It is", players[whoseTurn] + "'s turn. Note that", turns[whoseTurn], "is retarded."
        initial.append(squareToIndex(raw_input("Choose your initial square: ")))
        destination.append(squareToIndex(raw_input("Choose your destination square: ")))

    if "K" in board[secondCoordinate[0]][secondCoordinate[1]]:
        board[secondCoordinate[0]][secondCoordinate[1]] = board[firstCoordinate[0]][firstCoordinate[1]]
        board[firstCoordinate[0]][firstCoordinate[1]] = " "
        printBoard(board)
        print(players[whoseTurn] + " is a retard!")
        print(players[not whoseTurn] + " is slightly less retarded!")
        exit(0)
    board[secondCoordinate[0]][secondCoordinate[1]] = board[firstCoordinate[0]][firstCoordinate[1]]
    board[firstCoordinate[0]][firstCoordinate[1]] = " "
    printBoard(board)

